package br.com.pfc.application.dao;

import br.com.pfc.model.Endereco;
import br.com.pfc.model.Local;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.TransactionPropagationType;
import org.jboss.seam.annotations.Transactional;

/**
 *
 * @author mucceli
 */
@AutoCreate
@Name("localDAO")
@Transactional(TransactionPropagationType.SUPPORTS)
public class LocalDAO {

    @In
    private Session pfc;

    public Local obterPorId(int codLocal) {

        Criteria c = pfc.createCriteria(Local.class);
        c.add(Restrictions.eq("idLocal", codLocal));
        return (Local) c.uniqueResult();
    }

    public List<Local> obterTodos() {
        Criteria c = pfc.createCriteria(Local.class);
        return c.list();
    }

    public void alterar(Local local) {
        pfc.update(local);
        pfc.flush();
    }

    public void salvar(Local local) {
        pfc.save(local);
        pfc.flush();
    }

    public List<Local> pesquisar() {
        return obterTodos();
    }

    public List<Local> pesquisaPorNome(String nomeLocal) {
        Criteria c = pfc.createCriteria(Local.class);
        c.add(Restrictions.eq("nome", nomeLocal));
        return c.list();
    }
    
    public List<Local> pesquisaPorCidade(String nomeCidade) {
        Criteria c = pfc.createCriteria(Endereco.class);
        c.add(Restrictions.eq("cidade", nomeCidade));
        List<Endereco> listaEnderecos = c.list();
        List<Local> listaLocais = new ArrayList<Local>();
        for(Endereco endereco :listaEnderecos){
            Criteria c2 = pfc.createCriteria(Local.class);
            c2.add(Restrictions.eq("endereco", endereco.getIdEndereco()));
            listaLocais.addAll(c2.list());
        }
        return listaLocais;
    }
    public List<Local> pesquisaPorBairro(String nomeBairro) {
        Criteria c = pfc.createCriteria(Endereco.class);
        List<Endereco> listaEnderecos = (List<Endereco>) c.add(Restrictions.eq("bairro", nomeBairro));
        List<Local> listaLocais = new ArrayList<Local>();
        for(Endereco endereco :listaEnderecos){
            Criteria c2 = pfc.createCriteria(Local.class);
            c2.add(Restrictions.eq("endereco", endereco.getIdEndereco()));
            listaLocais.addAll(c2.list());
        }
        return listaLocais;
    }

    public List<Local> pesquisa() {
        Criteria c = pfc.createCriteria(Local.class);
        return c.list();
    }

    public void excluir(Local local) {
        pfc.delete(local);
        pfc.flush();
    }

    /**
     * Efetua FLUSH
     * @throws Exception
     */
    public void cancelar() throws Exception {

        try {
            pfc.clear();
            pfc.flush();
        } catch (HibernateException he) {
            throw new Exception(he);
        }
    }
}


